package com.kunbo.cn.app.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.service.OrderManagementService;

@Service
public class OrderManagementImpl implements OrderManagementService{
	
	@Autowired
	private JdbcTemplate jdbc;
	
	

	@Override
	public List<Map<String, Object>> getPurchaseOrder(Integer page,Integer pageSize,String purchaseOrder) {
		// TODO Auto-generated method stub
		String sql = "select * from bussiness_purchasing_order_manager  where 1 = 1 ";
		if(purchaseOrder != null && !purchaseOrder.isEmpty()) {
			sql += " and purchase_order_code like '%" + purchaseOrder + "%'";
		}
		sql += " order by id  limit " + (page * pageSize) +"," + pageSize;
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		/*for (Map<String, Object> map : list) {
			Integer material = Integer.parseInt(map.get("material_name").toString());
			String materialSql = "select name from business_materials_manage where id = " + material;
			List<Map<String, Object>> materialList = jdbc.queryForList(materialSql);
			if(materialList != null && materialList.size() > 0 ) {
			map.put("material_name", materialList.get(0).get("name").toString());
			map.put("material_name", materialList.get(0).get("name").toString());
			if(materialList.size()>0) {
				map.put("material_name", materialList.get(0).get("name").toString());
			}
			}else {
				map.put("material_name", null );
			}
		}*/
		return list;
	}

	@Override
	public List<Map<String, Object>> getSalesOrder(Integer page,Integer pageSize,String salesOrder) {
		// TODO Auto-generated method stub
		String sql = "select * from business_sale_order_manage where 1 = 1";
		if(salesOrder != null && !salesOrder.isEmpty()) {
			sql += " and sale_order_code like '%" + salesOrder + "%'";
		}
		sql += " order by id  limit " + (page * pageSize) +"," + pageSize;
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> purchaseDetails(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select * from bussiness_purchasing_order_manager where id = " + id;
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> salesOrderDetails(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select * from business_sale_order_manage where id = " + id;
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> homePage() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String sqlPurchasing = "select actual_quantity from bussiness_purchasing_order_manager";
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String, Object>> l = jdbc.queryForList(sqlPurchasing);
		float purchasing = 0;
		for (Map<String, Object> map1 : l) {
			if(map1.get("actual_quantity").toString() != null && !map1.get("actual_quantity").toString().isEmpty()  ) {
			purchasing += Float.parseFloat(map1.get("actual_quantity").toString());
		}
		}
		String sqlSale = "select lower_amount from business_sale_order_manage";
		l = jdbc.queryForList(sqlSale);
		float sale = 0;
		for (Map<String, Object> map1 : l) {
			if(map1.get("lower_amount").toString() != null && !map1.get("lower_amount").toString().isEmpty()  ) {
			sale += Float.parseFloat(map1.get("lower_amount").toString());
			}
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String sqlInventory = "select inventory from business_inventory_manage where curent_date like '%" + format.format(new Date()) + "%'";
		l = jdbc.queryForList(sqlInventory);
		float inventory = 0;
		for (Map<String, Object> map1 : l) {
			if(map1.get("inventory").toString() != null && !map1.get("inventory").toString().equals("")) {
			inventory += Float.parseFloat(map1.get("inventory").toString());
			}
		}
		map.put("purchasing", purchasing);
		map.put("sale", sale);
		map.put("inventory", inventory);
		list.add(map);
		return list;
	}

}
